package watch

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"log"
	"os/exec"
	"strings"
)

func main1() {

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}else {
		fmt.Println("开始运行!")
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					//log.Println("modified/**/ file:", event.Name)

					name:=event.String()
					//fmt.Println(name)

					index:=strings.Index(name, ".idea")

					//index2:=strings.Index(name,"___jb_tmp___")
					index3:=strings.Index(name,".git")

					//fmt.Println(index,"=====",name)
					if index==-1  &&index3==-1 {
						fmt.Println("这个不包含")
						reload()
					}else{

					}
				}


			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	//err = watcher.Add("/path/to/file1")
	//if err != nil {
	//	log.Fatal(err)
	//}
	err = watcher.Add("/Volumes/E/www/go/src/hello")//也可以监听文件夹
	if err != nil {
		log.Fatal(err)
	}
	<-done
}

func reload() {
	fmt.Println("=====重启=====");

	cmd := exec.Command("echo hello word!") //重启命令根据自己的需要自行调整
	fmt.Println(cmd.Run())
}
